﻿/**
 * ...
 * @author Natalia Ukhorskaya
 */

package gui.hug;

import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextFormat;

class HUGDynamicList extends HUGSprite
{
	private static inline var UP_BUTTON_NAME : String = "upButton";
	private static inline var DOWN_BUTTON_NAME : String = "downButton";
	
	private var elements : Array < HUGSprite >;
	private var elementsCount : Int;
	private var listHeight : Float;
	private var currentPage : Int;
	private var showFirstPage: Bool;
	
	private var upButton : HUGButton;
	private var downButton : HUGButton;
	private var label : HUGLabel;
	
	private var firstSpriteIndex: Array <Int>;
	private var lastSpriteIndex: Array <Int>;
	private var countOfPages: Int;
	
	private var showedElements : Array < HUGSprite>;
	
	public function new(listHeight : Float, elements : Array < HUGSprite >, ?showFirstPage: Bool = false) {
		super();
		this.listHeight = listHeight - 15;
		this.elementsCount = elements.length;
		this.elements = elements;
		this.showedElements = new Array < HUGSprite >();
		this.showFirstPage = showFirstPage;
		drawList();
	}
	
	private function drawList() {
		partition();
		if (showFirstPage) {
			currentPage = 1;
		} else {
			currentPage = countOfPages;
		}
			
		if (countOfPages != 1) {
			upButton = new HUGLinkButton("<", new TextFormat("Arial", 18, HUGColors.VK_BUTTON), HUGColors.WHITE);
			upButton.addEventListener(MouseEvent.CLICK, onUpButtonClick);
			upButton.setLocation(elements[1].width / 2 - 50 - upButton.width, listHeight - 2);
			upButton.name = UP_BUTTON_NAME;
			this.addChild(upButton);
			
			label = new HUGLabel();
			var lab: String;
			if (showFirstPage) {
				lab = "1";
			} else {
				lab = "" + countOfPages;
			}
			label.text = lab + " из " + countOfPages;
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			label.setLocation(elements[1].width / 2 - label.width / 2, listHeight - 0);
			this.addChild(label);
			
			downButton = new HUGLinkButton(">", new TextFormat("Arial", 18, HUGColors.VK_BUTTON), HUGColors.WHITE);
			downButton.addEventListener(MouseEvent.CLICK, onDownButtonClick);
			downButton.setLocation(elements[1].width / 2 + 50, listHeight - 2);
			downButton.name = DOWN_BUTTON_NAME;
			this.addChild(downButton);
		}
		if (showFirstPage) {
			showNewElements(1);
		} else {
			showNewElements(countOfPages);
		}
		
	}
	
	private function showNewElements(page: Int) {
		var firstVisible: Int = firstSpriteIndex[page - 1];
		var lastVisible: Int = lastSpriteIndex[page - 1];
		
		var height: Float = 0;
		var i : Int = firstVisible;
		while (i <= lastVisible) {
			elements[i].setLocation(0, height);
			showedElements.push(elements[i]);
			this.addChild(elements[i]);
			//trace("___"  + elements[i].height);
			if (elements[i].height > 70) {
				height += elements[i].height + 5;
			} else {
				height += 70 + 5;
			}
			
			i++;
		}
	}
	
	private function onUpButtonClick(e : MouseEvent) {
		if (currentPage > 1) {
			currentPage --;
			label.text = currentPage + " из " + countOfPages;	
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			removeCurrentElements();
			showNewElements(currentPage);
		}
	}
	
	private function onDownButtonClick(e : MouseEvent) {
		if (currentPage > 0 && currentPage < countOfPages) {
			currentPage ++;
			label.text = currentPage + " из " + countOfPages;	
			label.setTextFormat(new TextFormat("Arial", 13, HUGColors.VK_PROPERTY_NAME_TEXT));
			removeCurrentElements();
			showNewElements(currentPage);
		}
	}
	
	private function removeCurrentElements() {
		for (e in showedElements) {
			this.removeChild(e);
		}
		showedElements = new Array < HUGSprite >();
	}
	
	private function partition() {
		firstSpriteIndex = new Array();
		lastSpriteIndex = new Array();
		countOfPages = 1;
		
		var i: Int = 0;
		var height: Float = 0;
		if (elementsCount > 0) {
			firstSpriteIndex.push(0);
		}
		while (i < elementsCount) {
			if (elements[i].height > 70) {
				height += elements[i].height + 5;
			} else {
				height += 70 + 5;
			}
			if (height > listHeight) {
				lastSpriteIndex.push(i - 1);
				firstSpriteIndex.push(i);
				countOfPages ++;
				height = 0;
				i--;
			}
			i++;
		}
		lastSpriteIndex.push(elementsCount - 1);
	}
}